package com.logistics.container.controller;


import com.logistics.container.entity.model.ContainerPager;
import com.logistics.container.entity.model.ContainerRecovery;
import com.logistics.container.entity.model.ContainerSave;
import com.logistics.container.service.ContainerService;
import com.logistics.utils.aspect.annotation.LoggerListener;
import com.logistics.utils.constants.LoggerMethod;
import com.logistics.utils.exception.MessageException;
import com.logistics.utils.response.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 托盘主数据 前端控制器
 * </p>
 *
 * @author yunjian.ban
 * @since 2024-05-21
 */
@RestController
@RequestMapping("lgs/container")
public class ContainerController {

    @Autowired
    private ContainerService containerService;

    /**
     * 查找分页
     *
     * @param param
     * @return
     */
    @PostMapping("pager")
    @LoggerListener(businessName = "查询托盘列表", methodName = LoggerMethod.SEARCH)
    public ResponseEntity pager(@RequestBody ContainerPager param) {
        return ResponseUtil.success(containerService.pager(param));
    }

    /**
     * 查找所有托盘
     * @return
     */
    @GetMapping("listAll")
    public ResponseEntity listAll() {
        return ResponseUtil.success(containerService.listAll());
    }

    /**
     * 查找详细
     *
     * @param id
     * @return
     * @throws MessageException
     */
    @GetMapping("details/{id}")
    @LoggerListener(businessName = "查询托盘详细", methodName = LoggerMethod.SEARCH)
    public ResponseEntity details(@PathVariable("id") String id) throws MessageException {
        return ResponseUtil.success(containerService.details(id));
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     * @throws MessageException
     */
    @DeleteMapping("remove/{ids}")
    @LoggerListener(businessName = "删除托盘信息", methodName = LoggerMethod.DELETE)
    public ResponseEntity remove(@PathVariable("ids") String ids) throws MessageException {
        containerService.delete(ids);
        return ResponseUtil.success();
    }

    /**
     * 保存
     *
     * @param save
     * @return
     */
    @PutMapping("save")
    @LoggerListener(businessName = "保存托盘信息", methodName = LoggerMethod.SAVE)
    public ResponseEntity save(@RequestBody ContainerSave save) throws MessageException{
        containerService.save(save);
        return ResponseUtil.success();
    }

    /**
     * 空托回收
     * @param containerRecovery
     * @return
     */
    @LoggerListener(businessName = "托盘回收", methodName = LoggerMethod.SAVE)
    @PutMapping("/recovery")
    public ResponseEntity recovery(@RequestBody ContainerRecovery containerRecovery) throws MessageException {
        containerService.recovery(containerRecovery);
        return ResponseUtil.success();
    }

    /**
     * 空托呼叫
     * @param code
     * @param number
     * @return
     * @throws MessageException
     */
    @PutMapping("/call/{code}/{number}")
    @LoggerListener(businessName = "空托呼叫", methodName = LoggerMethod.SAVE)
    public ResponseEntity call(@PathVariable("code") String code, @PathVariable("number") String number) throws MessageException {
        containerService.call(code, Integer.parseInt(number));
        return ResponseUtil.success();
    }
}

